package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.mq.result.Result;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/admin/activity/couponInfo")
//@CrossOrigin
public class CouponInfoController {
    @Autowired
    private CouponInfoService couponInfoService;

//    //1.优惠卷分页查询
//    @GetMapping("{page}/{limit}")
//    public Result list(@PathVariable Long page, @PathVariable Long limit) {
//        IPage<CouponInfo> pageModel = couponInfoService.selectPageCouponInfo(page,limit);
//        return Result.ok(pageModel);
//    }

    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit
    ) {
        Page<CouponInfo> pageParam = new Page<>(page, limit);
        IPage<CouponInfo> pageModel = couponInfoService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    //2.添加优惠卷
    @PostMapping("save")
    public Result save(@RequestBody CouponInfo couponInfo) {
        couponInfoService.save(couponInfo);
        return Result.ok(null);
    }

    //3.根据id查询优惠卷
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        CouponInfo couponInfo = couponInfoService.getCouponInfo(id);
        return Result.ok(couponInfo);
    }
    //4.根据优惠卷id查询规则数
    @GetMapping("findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable Long id){
        Map<String,Object> map=couponInfoService.findCouponRuleList(id);
        return Result.ok(map);
    }
    //5.添加优惠卷规则数据
    @PostMapping("saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo){
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }


    @PutMapping("update")
    @ApiOperation(value = "修改")
    public Result updateById(@RequestBody CouponInfo couponInfo) {
        couponInfoService.updateById(couponInfo);
        return Result.ok(null);
    }

    //删除
    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "单个删除")
    public Result removeById(@PathVariable("id") Long id) {
        couponInfoService.removeById(id);
        return Result.ok(null);
    }

    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除")
    public Result batchRemove(@RequestBody List<Long> ids) {
        couponInfoService.removeByIds(ids);
        return Result.ok(null);
    }
}

